<?php
class IndexAction extends CommonAction {

	//清空缓存
	public function clearCache($dir = "all")
	{
		import("@.ORG.Dir");
		$obj_dir = new Dir;
		is_dir(LOG_PATH) && $obj_dir->delDir(LOG_PATH);
		if ($dir == "all") {
			is_dir(DATA_PATH . '_fields/') && $obj_dir->del(DATA_PATH . '_fields/');
			is_dir(CACHE_PATH) && $obj_dir->delDir(CACHE_PATH);
			is_dir(DATA_PATH) && $obj_dir->del(DATA_PATH);
			is_dir(TEMP_PATH) && $obj_dir->delDir(TEMP_PATH);
			@unlink(RUNTIME_FILE);
		}elseif ($dir == "tpl") {
			is_dir(CACHE_PATH) && $obj_dir->delDir(CACHE_PATH);
		}elseif ($dir == "data") {
			is_dir(DATA_PATH . '_fields/') && $obj_dir->del(DATA_PATH . '_fields/');
			is_dir(DATA_PATH) && $obj_dir->del(DATA_PATH);
			is_dir(TEMP_PATH) && $obj_dir->delDir(TEMP_PATH);
			@unlink(RUNTIME_FILE);
		}
		
		$this->success('操作成功');
	}

	public function index()
	{
		$map['mid'] = $this->mid;
		$Michome = M('Michome');
		$michome = $Michome->where($map)->find();
		if (!$michome) {
			$data['mid'] = $this->mid;
			$Michome->add($data);
		}

		$Mpaccount = M('Mpaccount');
		$mp = $Mpaccount->where($map)->find();
		if (!$michome) {
			$data['mid'] = $this->mid;
			$Mpaccount->add($data);
		}
		$this->display();
	}

	public function filebox()
	{
		$Ablum = D('Ablum');
		$map['mid'] = $this->mid;
		$this->_list($Ablum,$map);
		$this->display();
	}

	public function upimg()
	{
		$ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest';

		$result = array();

		$file = $_FILES['avatar'];
		if(!preg_match('/^image\//' , $file['type']) 
		|| !preg_match('/\.(jpe?g|gif|png|jpg|jpeg|JPG)$/' , $file['name'])
			|| getimagesize($file['tmp_name']) === FALSE
		) {
			$result['status'] = 'ERR';
			$result['message'] = '非法的文件格式';
		}
		else if($file['size'] > 220000) {
			$result['status'] = 'ERR';
			$result['message'] = '文件大于200kb!';
		}
		else if($file['error'] != 0 || !is_uploaded_file($file['tmp_name'])) {
			$result['status'] = 'ERR';
			$result['message'] = '未知错误!';
		}
		else {
			$pathStr = './Upload/'.$this->mid.'/';
	        if ( strrchr( $pathStr , "/" ) != "/" ) {
	            $pathStr .= "/";
	        }
	        $pathStr .= date( "Y/m/d" );
	        if ( !file_exists( $pathStr ) ) {
	            if ( !mkdir( $pathStr , 0777 , true ) ) {
	                return false;
	            }
	        }
	        $getFileExt = strtolower( strrchr( $file[ "name" ] , '.' ) );
	        $nfilename = time().rand( 1 , 10000 ).$getFileExt;
			$save_path = $pathStr.'/'.$nfilename;
			// $thumb_path = 'thumb.jpg';
			if( 
				! move_uploaded_file($file['tmp_name'] , $save_path)
				// OR
				// !resize($save_path, $thumb_path, 150) 
			  )
				{
				$result['status'] = 'ERR';
				$result['message'] = '文件保存失败!';
				}else {
					$result['status'] = 'OK';
					$result['message'] = '上传成功!';

					$save_path = str_replace('./', '', $save_path);
					$result['url'] = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']).'/'.$save_path;
					$Ablum = M('Ablum');
					$data['mid'] = $this->mid;
					$data['size'] = $file['size'];
					$data['picurl'] = $result['url'];
					$data['picname'] = $file['name'];
					$Ablum->add($data);
			}
		}
		$result = json_encode($result);
		if($ajax) {
			echo $result;
		}
		else {
		echo '<script language="javascript" type="text/javascript">';
		echo 'var iframe = window.top.window.jQuery("#'.$_POST['temporary-iframe-id'].'").data("deferrer").resolve('.$result.');';
		echo '</script>';
		}
	}
}